package coco.util
{
	import coco.component.window.WindowComment;
	import coco.component.window.WindowDirect;
	import coco.component.window.WindowImage;
	import coco.component.window.WindowInfo;
	import coco.component.window.WindowReplyComment;
	import coco.component.window.WindowRepost;
	import coco.component.window.WindowSearch;
	import coco.component.window.WindowSendMsg;
	import coco.component.window.WindowSetting;
	import coco.window.MainContent;
	
	import cocoBlog.data.BlogComment;
	import cocoBlog.data.BlogDirectMessage;
	import cocoBlog.data.BlogStatus;
	import cocoBlog.data.BlogUser;
	
	import flash.display.NativeWindowType;
	import flash.events.Event;
	
	import mx.core.FlexGlobals;

	public class WindowUtil
	{
		private static var instance:WindowUtil;
		
		public static function getInstance():WindowUtil
		{
			if(!instance)
				instance = new WindowUtil();
			
			return instance;
		}
		
		
		//-----------------------------------------------------------------------------------------
		//    NewWindow
		//-----------------------------------------------------------------------------------------
		public function openRepostWindow(data:BlogStatus):WindowRepost
		{
			var repostWindow:WindowRepost = new WindowRepost();
			repostWindow.open();
			repostWindow.data = data;
			repostWindow.orderToFront();
			
			return repostWindow;
		}
		
		//-----------------------------------------------------------------------------------------
		//    Search Window
		//-----------------------------------------------------------------------------------------
		public function openSearchWindow():WindowSearch
		{
			var searchWindow:WindowSearch = new WindowSearch();
			searchWindow.open();
			searchWindow.orderToFront();
			
			return searchWindow;
		}
		
		
		private var sw:WindowSetting;
		/**
		 * 打开设置窗口
		 * @return 
		 */		
		public function openSettingWindow():WindowSetting
		{
			if(!sw)
			{
				sw = new WindowSetting();
				sw.addEventListener(Event.CLOSE, settingWindowCloseHandler);
				sw.open();
			}
			else
				sw.activate();
			
			return sw;
		}
		
		private function settingWindowCloseHandler(e:Event):void
		{
			sw.removeEventListener(Event.CLOSE,settingWindowCloseHandler);
			sw = null;
		}
		
		
		//-----------------------------------------------------------------------------------------
		//    Comment Window
		//-----------------------------------------------------------------------------------------
		public function openCommentWindow(blogStatus:BlogStatus):WindowComment
		{
			var commentWindow:WindowComment = new WindowComment();
			commentWindow.open();
			commentWindow.orderToFront();
			commentWindow.data = blogStatus;
			return commentWindow;
		}
		
		
		//-----------------------------------------------------------------------------------------
		//    Comment Window
		//-----------------------------------------------------------------------------------------
		public function openReplyCommentWindow(data:BlogComment):WindowReplyComment
		{
			var replyCommentWindow:WindowReplyComment = new WindowReplyComment();
			replyCommentWindow.open();
			replyCommentWindow.orderToFront();
			replyCommentWindow.data = data;
			return replyCommentWindow;
		}
		
		
		//-----------------------------------------------------------------------------------------
		//    ImageWindow
		//-----------------------------------------------------------------------------------------
		public function openImageWindow(source:String):WindowImage
		{
			var imageWindow:WindowImage = new WindowImage();
			imageWindow.open();
			imageWindow.source = source;
			imageWindow.orderToFront();
			
			return imageWindow;
		}
		
		
		//-----------------------------------------------------------------------------------------
		//    sendMsg
		//-----------------------------------------------------------------------------------------
		public function openSendMsgWindow():WindowSendMsg
		{
			var sendMsgWindow:WindowSendMsg = new WindowSendMsg();
			sendMsgWindow.open();
			sendMsgWindow.orderToFront();
			
			return sendMsgWindow;
		}
		
		//-----------------------------------------------------------------------------------------
		//    NEW DIRECT
		//-----------------------------------------------------------------------------------------
		public function openDirectWindow(data:BlogUser):WindowDirect
		{
			var directWindow:WindowDirect = new WindowDirect();
			directWindow.open();
			directWindow.userData = data;
			directWindow.orderToFront();
			
			return directWindow;
		}
		
		
		//-----------------------------------------------------------------------------------------
		//    NEW DIRECT
		//-----------------------------------------------------------------------------------------
		public function openMessageWindow(data:BlogDirectMessage, state:String):WindowDirect
		{
			var messageWindow:WindowDirect = new WindowDirect();
			messageWindow.open();
			messageWindow.currentState = state;
			messageWindow.messageData = data;
			messageWindow.orderToFront();
			
			return messageWindow;
		}
		
		
		/**
		 * 浮现窗口的形式 提示用户
		 * */
		public static function toolTip(msg:String):void
		{
		   FlexGlobals.topLevelApplication['tip']['show'](msg);
		}
		
		public static function load(value:Boolean):void
		{
			FlexGlobals.topLevelApplication['gLoad']['visible'] = value;
		}
		
		public static function progress(value:int):void
		{
			FlexGlobals.topLevelApplication['gProgress']['setValue'](value);
		}
		
		private var infoWindow:WindowInfo;
		public function OpenInfoWindow():WindowInfo
		{
			if(!infoWindow)
			{
				infoWindow = new WindowInfo();
				infoWindow.type = NativeWindowType.LIGHTWEIGHT;
				infoWindow.open(false);
				infoWindow.visible = false;
				infoWindow.addEventListener(Event.CLOSE, infoClosed);
			}
			return infoWindow;
		}
		
		
		private function infoClosed(e:Event):void
		{
			infoWindow.removeEventListener(Event.CLOSE, infoClosed);
			infoWindow = null;
		}
		
		
		public var mainContent:MainContent;
		/**
		 * MainContent
		 * 
		 *  0  : main
		 *  1  : user
		 *  2  : friends
		 * */
		public function setMainContentIndex(index:int):void
		{
			if(mainContent)
				mainContent.setIndex(index);
		}
		
		
		public function getNextPage():void
		{
			if(mainContent)
				mainContent.getNextPage();
		}
		
		
		public function getBackPage():void
		{
			if(mainContent)
				mainContent.getBackPage();
		}
		
		
		public function updatePage():void
		{
			if(mainContent)
				mainContent.updatePage();
		}
		
		
		/**Main content 添加微博消息 针对主页*/
		public function addStatus(statuses:Array):void
		{
			if(mainContent)
				mainContent.main.gMain.display(statuses);
		}
	}
}